package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.AdminAddress;
import com.gaogzhen.domain.Coin;
import com.gaogzhen.model.ExceptionEnum;
import com.gaogzhen.service.AdminAddressService;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description
 * @date 2022-11-07 10:46
 */
@RestController
@RequestMapping("/adminAddress")
@Api(tags = "归集地址控制器")
public class AdminAddressController {

    @Autowired
    private AdminAddressService adminAddressService;

    @GetMapping
    @ApiOperation(value = "分页查询归集地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId", value = "币种id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
    })
    public R<Page<AdminAddress>> findByPage(@ApiIgnore Page<AdminAddress> page, Long coinId) {
        return R.ok(adminAddressService.findByPage(page, coinId));
    }


    @PostMapping
    @ApiOperation("新增归集地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminAddress", value = "adminAddress json数据")
    })
    public R add(@RequestBody @Validated AdminAddress adminAddress) {
        SqlExecuteUtils.sqlExceptionHandler(a ->adminAddressService.save(a), adminAddress, ExceptionEnum.COIN_TYPE_ADD_FAIL);

        return R.ok();
    }

    @PatchMapping
    @ApiOperation("修改归集地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminAddress", value = "adminAddress json数据")
    })
    public R update(@RequestBody @Validated AdminAddress adminAddress) {
        SqlExecuteUtils.sqlExceptionHandler(a ->adminAddressService.updateById(a), adminAddress, ExceptionEnum.COIN_TYPE_UPDATE_FAIL);

        return R.ok();
    }
}
